<template>
  <div class="spl-container">
    <breadcrumb :data="pathData"></breadcrumb>
    <div class="emp-container">
      <div class="search-row m-form clearfix">
        <div class="fl">
          <el-checkbox-group v-model="search.jobStatus" size="small">
            <el-checkbox label="1" border style="margin-right: 10px;">在职</el-checkbox>
            <el-checkbox label="2" border>离职</el-checkbox>
          </el-checkbox-group>
        </div>
        <el-select v-model="search.withholdingAgent" placeholder="请选择扣缴义务人" filterable class="w-220 ml-30">
          <el-option
            v-for="item in obligorOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>

        <el-date-picker
          v-model="search.incomeDateStr"
          type="month" value-format="yyyy-MM" class="label-sel w-120" style="margin-left: 30px;">
        </el-date-picker>

        <el-input placeholder="搜索姓名/证件号/手机号" clearable v-model="search.selectMsg" class="ml-30 w-200"></el-input>
        <div class="fr">
          <el-button type="primary" class="opt-btn ml-20 s-btn" @click="getTableData" icon="icon el-icon-search">查询</el-button>
          <el-button class="opt-btn yellow-btn ml-20 s-btn"  @click="importVisible=true"><i class="icon ic-import"></i>导入</el-button>
        </div>

      </div>

      <dTable @fetch="getTableData" ref="empTable" :tableHeight="tableHeight" :showIndex="true" :showSelection="true">
        <el-table-column prop="empName" label="员工姓名" width="130" header-align="center" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="empIdCard" label="证件号码" width="180" header-align="center" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="phoneNumber" label="手机号码" width="180" header-align="center" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="onJobState" label="在职状态" width="130" header-align="center" align="left" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.onJobState==1">在职</span>
            <span v-if="scope.row.onJobState==2">离职</span>
          </template>
        </el-table-column>
        <el-table-column prop="entryDateStr " label="入职日期" width="130" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="leaveDateStr" label="离职日期" width="130" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="withholdingAgentName" label="扣缴义务人" width="300" header-align="center" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="incomeDateStr" label="收入所属期" header-align="center" width="130" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="childEducation" label="累计子女教育" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="continueEducation" label="累计继续教育" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="medicalCare" label="累计大病医疗" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="supportOld" label="累计赡养老人" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="houseLoan" label="累计住房贷款利息" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="houseRent" label="累计住房租金" header-align="center" width="200" align="right" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="employeementDate" label="任职受雇年月" header-align="center" min-width="200" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-text="scope.row.employeementDate ? $moment(scope.row.employeementDate).format('YYYY-MM') : ''"></span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
          <template slot-scope="scope">
            <div class="table-operate-btns">
              <el-button type="text" icon="el-icon-edit" class="btn edit-btn" @click="setInfo(scope.row)">设置累计值</el-button>
            </div>
          </template>
        </el-table-column>
        <template slot="pagination-btns">
          <el-button type="primary" class="opt-btn s-btn" icon="ic-export" @click="exportData">导出</el-button>
        </template>
      </dTable>
    </div>

    <el-dialog title="设置累计值" class="spl-dialog setting-spl-dialog" :visible.sync="settingVisible" :closeOnClickModal="false" width="1400px">
      <div class="clearfix collectDeduct-dialog">
        <div class="setting-l">
          <div class="headImg-info clearfix">
            <div class="fl">
              <img :src="'/api/'+setInfoRow.licenseUrl" alt="" class="headImg">
            </div>
            <div class="fl ml-10">
              <!--1.在职 2.离职 -->
              <p><span v-text="setInfoRow.empName"></span>
                <span class="tag" v-if="setInfoRow.onJobState==1">在职</span>
                <span class="tag" v-if="setInfoRow.onJobState==2">离职</span>
              </p>
              <p class="mt-5" v-text="setInfoRow.deptName"></p>
              <p class="mt-5">入职日期：<span v-text="setInfoRow.entryDateStr"></span></p>
            </div>
          </div>

          <el-row :gutter="24" class="mt-10">
            <el-col :span="8"><p>当前收入所属期：<span v-text="setInfoRow.incomeDateStr"></span></p></el-col>
            <el-col :span="8"><p>扣缴义务人：<span v-text="setInfoRow.withholdingAgentName ? setInfoRow.withholdingAgentName : this.search.withholdingAgentName"></span></p></el-col>
            <el-col :span="8"><p>任职受雇年月：<span v-text="setInfoRow.employeementDate ? $moment(setInfoRow.employeementDate).format('YYYY-MM') : this.search.incomeDateStr"></span></p></el-col>
          </el-row>
          <el-form :model="settingForm" ref="settingForm" label-width="200px">
            <div class="pr-15">
              <div class="border-type mt-10">
                <p class="fw-blod">累计个税专项附加扣除：<span class="text-blue"><span v-text="settingForm.childEducation + settingForm.continueEducation + settingForm.supportOld
                                                                                              + settingForm.medicalCare + settingForm.houseLoan + settingForm.houseRent"></span></span></p>
                <el-row :gutter="24" class="mt-5">
                  <el-col :span="12">
                    <el-form-item label="累计子女教育:" prop="childEducation" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkEdu,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.childEducation" ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计继续教育:" prop="continueEducation" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.continueEducation"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="累计赡养老人:" prop="supportOld" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.supportOld"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计大病医疗:" prop="medicalCare" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.medicalCare"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="累计住房贷款利息:" prop="houseLoan" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkHouse,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.houseLoan"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计住房租金:" prop="houseRent" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkHouse,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.houseRent"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>

              <div class="border-type mt-10">
                <p class="fw-blod">累计上月扣除</p>
                <el-row :gutter="24" class="mt-5">
                  <el-col :span="12">
                    <el-form-item label="累计应纳税所得额（上月）:" prop="payableIncomeTaxLast" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.payableIncomeTaxLast"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计已预缴税额（上月）:" prop="payedTaxAdvanceLast" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.payedTaxAdvanceLast"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="24">
                    <el-form-item label="累计专项扣除（上月）:" prop="cumulativeSpecialDeduction" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.cumulativeSpecialDeduction"></el-input>
                      <span class="text-orange">（即社保公积金扣除）</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="累计子女教育（上月）:" prop="childEducationLast" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkEdu,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.childEducationLast"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计继续教育（上月）:" prop="continueEducationLast" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.continueEducationLast"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="累计赡养老人（上月）:" prop="supportOldLast" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.supportOldLast"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计大病医疗（上月）:" prop="medicalCareLast" class="pay-form-item" :rules="{validator:this.validAmount,trigger:'blur'}">
                      <el-input class="set-input" v-model.number="settingForm.medicalCareLast"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="累计住房贷款利息（上月）:" prop="houseLoanLast" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkHouse,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.houseLoanLast"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计住房租金（上月）:" prop="houseRentLast" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkHouse,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.houseRentLast"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>

              <div class="border-type mt-10">
                <p class="fw-blod">累计减除费用</p>
                <el-row :gutter="24" class="mt-5">
                  <el-col :span="12">
                    <el-form-item label="累计减除费用:" prop="totalReductionAmount" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkReduct,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.totalReductionAmount"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="累计减除费用（上月）:" prop="totalReductionAmountLast" class="pay-form-item" :rules="[{validator:this.validAmount,trigger:'blur'}, {validator:this.checkReduct,trigger:'blur'}]">
                      <el-input class="set-input" v-model.number="settingForm.totalReductionAmountLast"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-form>
          <div slot="footer" class="dialog-footer operate-btns-bottom mt-15 pb-15">
            <el-button @click="settingVisible=false">取 消</el-button>
            <el-button type="primary" style="margin-left: 60px;" :loading="settingBtnLoading" @click="saveSetInfo">确定保存</el-button>
          </div>
        </div>
        <div class="setting-r">
          <el-timeline v-if="operateHistory.length>0">
            <el-timeline-item v-for="(item,index) in operateHistory" :key="index" :timestamp="item.date" placement="top">
              <el-card>
                <p v-for="it in item.value" :key="it.id">
                  <span v-text="it.type==0?'单个：':'批量：'"></span>
                  <span v-text="it.itemTypeText"></span>修改金额为
                  <span v-text="$global.milliFormat(it.newAmount, 0)"></span>
                </p>
              </el-card>
            </el-timeline-item>
          </el-timeline>
          <p v-else style="text-align: center;margin-top: 30px;">暂无操作记录</p>
        </div>
      </div>
    </el-dialog>

    <el-dialog title="导入累计扣除数" class="spl-dialog setting-spl-dialog import-spl-dialog" :visible.sync="importVisible" :closeOnClickModal="false" width="90%">
      <div class="clearfix import-dialog">
        <div class="mt-15">
          <el-row :gutter="24">
            <el-col :span="10">
              <div @click="checkImportwithholdingAgent">
              <el-upload :disabled="uploadDisabled"
                class="upload-demo text-center"
                drag
                action="/api/sys/no-limit/upload/single"
                :headers="this.$global.setHeaders"
                :auto-upload="true"
                :multiple="false"
                :show-file-list="false"
                :on-success="uploadSuccess"
              :before-upload="beforeUpload">
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              </el-upload></div>
            </el-col>
            <el-col :span="14">
              <p class="text">1、若是首次使用系统，请上传本单位上一收入所属期税局税款计算表，以获取上月累计值</p>
              <p class="text">可参考表：<span class="text-blue f-cursor" @click="exportTemplate(1)">税款计算_工资薪金所得.xlsx</span></p>
              <p class="text">2、若员工已在系统正常申报个税，可上传本单位当前所属期税局综合申报表，以获取本月累计扣除数</p>
              <p class="text">或 下载右侧模板填充数据并导入：<span class="text-blue f-cursor" @click="exportTemplate(3)">累计专项附加扣除导入</span></p>
              <p class="text">请确保数据精确，保存成功后将无法进行修改</p>
              <p class="text">4、两份表格请勿改动表头，否则系统无法正确匹配您上传的数据</p>
              <p class="text text-orange" v-if="fileName">文件名：<span class="text-left mt-15" v-text="fileName"></span></p>
            </el-col>
          </el-row>
        </div>

        <div class="mt-10 clearfix">
          <el-select v-model="importFormData.withholdingAgent" @change="handleUploadDisabled" filterable clearable class="w-300 fl" placeholder="请选择扣缴义务人">
            <el-option
              v-for="item in obligorOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <p class="ml-60 fl lh-com">结果：成功<span v-text="importCount.successCount?importCount.successCount:0"></span>条&nbsp;&nbsp;&nbsp;&nbsp;失败<span class="text-red" v-text="importCount.failedCount?importCount.failedCount:0"></span>条
            <span class="ml-100 mr-10 inlineBlock">只看失败</span>
            <!--0：查看所有，1：只看失败-->
            <el-switch v-model="importFormData.type" :activeColor="$global.switchColor[0]"
                       :inactiveColor="$global.switchColor[1]" :active-value="1"
                       :inactive-value="0"  @change="getImportTableData">
            </el-switch>
          </p>
        </div>
        <div>
          <dTable @fetch="getImportTableData" ref="importTable" :tableHeight="importTableHeight" :show-index="true">
            <el-table-column prop="empName" label="员工姓名" width="220" header-align="center" align="left"></el-table-column>
            <el-table-column prop="empIdCard" label="证件号码" width="220" header-align="center" align="left"></el-table-column>
            <el-table-column prop="phoneNumber" label="手机号码" width="220" header-align="center" align="left"></el-table-column>
            <el-table-column prop="failedReason" label="备注" width="300" header-align="center" align="left"></el-table-column>
            <el-table-column prop="incomeDateStr" label="收入所属期" width="220" header-align="center" align="center"></el-table-column>
            <el-table-column prop="childEducation" label="累计子女教育" width="220" header-align="center" align="right"></el-table-column>
            <el-table-column prop="continueEducation" label="累计继续教育" width="220" header-align="center" align="right"></el-table-column>
            <el-table-column prop="medicalCare" label="累计大病医疗" width="220" header-align="center" align="right"></el-table-column>
            <el-table-column prop="supportOld" label="累计赡养老人" width="220" header-align="center" align="right"></el-table-column>
            <el-table-column prop="houseLoan" label="累计住房贷款利息" width="220" header-align="center" align="right"></el-table-column>
            <el-table-column prop="houseRent" label="累计住房租金" min-width="220" header-align="center" align="right"></el-table-column>
            <template slot="pagination-btns">
              <el-button type="primary" class="opt-btn s-btn" icon="ic-export" @click="exportImportData">导出</el-button>
            </template>
          </dTable>
        </div>
        <div slot="footer" class="dialog-footer operate-btns-bottom mt-15 pb-15">
          <el-button @click="importVisible=false">取 消</el-button>
          <el-button type="primary" style="margin-left: 60px;" :loading="importBtnLoading" @click="saveImport">确定保存</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import dTable from '../../../components/common/table'
export default {
  components: { dTable },
  data () {
    return {
      // 导航
      pathData: [
        { name: '薪酬管理' },
        { name: '收集累计扣除数' }
      ],
      settingVisible: false,
      settingBtnLoading: false,
      obligorOptions: [],
      search: {
        jobStatus: ['1'], // 1.在职 2.离职
        withholdingAgent: '',
        withholdingAgentName: '',
        incomeDateStr: this.$moment(new Date()).format('YYYY-MM'),
        selectMsg: ''
      },

      settingForm: {
        childEducation: 0, // 累计子女教育
        childEducationLast: 0,
        continueEducation: 0, // 累计继续教育
        continueEducationLast: 0,
        cumulativeSpecialDeduction: 0, // 累计专项扣除（上月）
        deductId: '',
        deductIdLast: '',
        employeeId: '',
        houseLoan: 0, // 累计住房贷款利息
        houseLoanLast: 0,
        houseRent: 0, // 累计住房租金
        houseRentLast: 0,
        incomeDateStr: '',
        medicalCare: 0,
        medicalCareLast: 0,
        payableIncomeTaxLast: 0, // 累计应纳税所得额（上月）
        payedTaxAdvanceLast: 0, // 累计已预缴税额（上月）
        supportOld: 0, // 累计赡养老人
        supportOldLast: 0,
        totalReductionAmount: 0, // 累计减除费用
        totalReductionAmountLast: 0,
        withholdingAgent: '' // 扣缴义务人，筛选条件中传入
      },

      uploadDisabled: true,
      importBtnLoading: false,
      importVisible: false,
      fileName: '',
      importFormData: {
        fileId: '',
        withholdingAgent: '',
        type: '0' // 只看失败
      },
      importCount: {},
      setInfoRow: {},
      operateHistory: [] // 操作历史
    }
  },
  created () {
    this.getObligorData() // 获取扣缴义务人数据
    this.$nextTick(() => {

    })
  },
  computed: {
    tableHeight: function () {
      return this.$global.bodyHeight - 220 + 'px'
    },
    importTableHeight: function () {
      return this.$global.bodyHeight - 400 + 'px'
    }
  },
  methods: {
    // 获取表格数据
    getTableData (params = []) {
      if (this.search.incomeDateStr == '' || this.search.incomeDateStr == null) {
        this.$message.warning('请先选择收入所属期！')
        return
      }
      params =
          [
            { property: 'jobStatus', value: this.search.jobStatus },
            { property: 'withholdingAgent', value: this.search.withholdingAgent },
            { property: 'incomeDateStr', value: this.search.incomeDateStr },
            { property: 'selectMsg', value: this.search.selectMsg }
          ]
      this.$refs.empTable.fetch(params, 'post', '/employeecumulativededuct/page', function (data) {
        if (!data.code == 0) {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      })
    },

    // 获取扣缴义务人数据
    getObligorData () {
      this.$http({
        url: '/company/withholdingAgent/page',
        method: 'get',
        params: {}
      }).then(({ data }) => {
        this.obligorOptions = data.rows
        this.search.withholdingAgent = data.rows[0].id
        this.search.withholdingAgentName = data.rows[0].name
        this.getTableData() // 获取表格数据
      })
    },
    // 导出--列表
    exportData () {
      var requestParams = this.$refs.empTable.getParamsObj()
      var obj = {
        withholdingAgent: requestParams.withholdingAgent,
        incomeDateStr: requestParams.incomeDateStr,
        selectMsg: requestParams.selectMsg,
        jobStatus: requestParams.jobStatus,
        deductIds: this.$refs.empTable.selections
      }
      this.$downloadFile('/employeecumulativededuct/pageExport', 'post', this.$qs.stringify(obj), this.$global.EXCEL)
    },

    // 导出--导入列表
    exportImportData () {
      if (this.importFormData.fileId != '') {
        var requestParams = this.$refs.importTable.getParamsObj()
        this.$downloadFile('/employeecumulativededuct/importPageExport', 'post', this.$qs.stringify(requestParams), this.$global.EXCEL)
      } else {
        this.$message({
          message: '请先上传文件',
          type: 'error'
        })
      }
    },

    //  导出模板
    exportTemplate (tempTag) {
      // tempTag  1：税款计算_工资薪金所得；2：累计综合所得申报表；3：累计专项附加扣除
      // type     1：否，导出空白模板；2：是，导出含有人员信息的模板
      this.$confirm('数据收集对象为当前客户所有员工（包括离职员工），是否导出这批员工的手机号、姓名、证件号码与在职状态?', '提示', {
        confirmButtonText: '是，导出员工名单',
        cancelButtonText: '否，导出空白模板',
        type: 'info',
        center: true,
        distinguishCancelAndClose: true
      }).then(() => {
        //  是，导出员工名单
        this.$downloadFile('/employeecumulativededuct/templateDownload/' + tempTag + '/2', 'get', {},
          this.$global.EXCEL)
      }).catch(action => {
        //  否，导出空白模板$downloadFile
        if (action == 'cancel') {
          this.$downloadFile('/employeecumulativededuct/templateDownload/' + tempTag + '/1', 'get', {},
            this.$global.EXCEL)
        }
      })
    },

    checkImportwithholdingAgent () {
      if (this.importFormData.withholdingAgent == '') {
        this.$message.warning('请先选择扣缴义务人')
      }
    },
    handleUploadDisabled (val) {
      if (val != '' && val != null) {
        this.uploadDisabled = false
      } else {
        this.uploadDisabled = true
      }
    },

    // 上传成功后
    uploadSuccess (response, file, fileList) {
      var fileId = response.data.id// 文件id
      this.$http({
        url: '/employeecumulativededuct/doImport',
        method: 'post',
        params: {
          fileId: fileId,
          withholdingAgent: this.importFormData.withholdingAgent
        }
      }).then(({ data }) => {
        if (data.code == 0) {
          this.fileName = file.name
          this.importFormData.fileId = fileId
          // 获取导入条数
          this.selectImportCount()
          // 获取导入列表数据
          this.getImportTableData()
        } else {
          this.$message({
            message: this.$global.errorMsg,
            type: 'error'
          })
        }
      }).catch((data) => {
        this.$message({
          message: '系统异常',
          type: 'error'
        })
      })
    },
    beforeUpload (file) {
      if (this.importFormData.withholdingAgent == '') {
        this.$message({
          message: '请先选择扣缴义务人',
          type: 'warning'
        })
        return false
      }
    },

    // 查询条数
    selectImportCount (params = []) {
      this.$http({
        url: '/employeecumulativededuct/selectImportCount',
        method: 'post',
        params: {
          fileId: this.importFormData.fileId
        }
      }).then(({ data }) => {
        if (data.code == 0) {
          this.importCount = data.data
        } else {
          this.$message({
            message: this.$global.errorMsg,
            type: 'error'
          })
        }
      }).catch((data) => {
        this.$message({
          message: '系统异常',
          type: 'error'
        })
      })
    },

    // 获取导入表格数据
    getImportTableData (type) {
      if (type == 'clear' || this.importFormData.fileId != '') {
        var params =
          [
            { property: 'fileId', value: this.importFormData.fileId },
            { property: 'type', value: Number(this.importFormData.type) }
          ]
        this.$refs.importTable.fetch(params, 'post', '/employeecumulativededuct/importPage', function (data) {
          if (!data.code == 0) {
            this.$message({
              message: this.$global.errorMsg,
              type: 'error'
            })
          }
        })
      }
    },

    // 保存导入
    saveImport () {
      this.importBtnLoading = true
      var fileId = this.importFormData.fileId
      if (fileId == '') {
        this.$message({
          message: '请先上传文件！',
          type: 'warning'
        })
        this.importBtnLoading = false
        return
      }
      this.$http({
        url: '/employeecumulativededuct/importSave',
        method: 'post',
        params: {
          fileId: fileId
        }
      }).then(({ data }) => {
        this.importBtnLoading = false
        if (data.code == 0) {
          this.importVisible = false
          this.$message.success('保存成功！')
          //  清空数据
          this.importFormData.fileId = ''
          this.importFormData.withholdingAgent = ''
          this.importFormData.type = '0'
          this.fileName = ''
          this.importCount = {}
          this.getImportTableData('clear')
        } else {
          this.importBtnLoading = false
          this.$message({
            message: this.$global.errorMsg,
            type: 'error'
          })
        }
      }).catch((data) => {
        this.importBtnLoading = false
        this.$message({
          message: '系统异常',
          type: 'error'
        })
      })
    },

    // 设置累计值
    setInfo (row) {
      var res = row
      if (res.withholdingAgent == '' || res.withholdingAgent == null || res.withholdingAgent == undefined) {
      // 行记录没有扣缴义务人
        if (this.search.withholdingAgent == '') {
          this.$message({
            message: '请先选择扣缴义务人',
            type: 'warning'
          })
          return false
        } else {
          this.settingForm.withholdingAgent = this.search.withholdingAgent // 行记录扣缴义务人
        }
      } else {
        this.settingForm.withholdingAgent = res.withholdingAgent // 行记录扣缴义务人
      }
      this.setInfoRow = row
      // （当月）设置累计值页面数据
      this.settingForm.deductId = res.id // 行记录id
      this.settingForm.incomeDateStr = res.incomeDateStr // 行记录id
      this.settingForm.employeeId = res.employeeId // 行记录id
      this.settingForm.childEducation = res.childEducation ? res.childEducation : 0 // 累计子女教育
      this.settingForm.continueEducation = res.continueEducation ? res.continueEducation : 0 // 累计继续教育
      this.settingForm.houseLoan = res.houseLoan ? res.houseLoan : 0 // 累计住房贷款利息
      this.settingForm.houseRent = res.houseRent ? res.houseRent : 0 // 累计住房租金
      this.settingForm.medicalCare = res.medicalCare ? res.medicalCare : 0 // 大病医疗
      this.settingForm.supportOld = res.supportOld ? res.supportOld : 0 // 累计赡养老人
      this.settingForm.totalReductionAmount = res.totalReductionAmount ? res.totalReductionAmount : 0 //累计减除费用
      // 获取（上月）设置累计值页面数据
      this.getRowDetail(res.employeeId, res.incomeDateStr, this.search.withholdingAgent)
      // 获取修改历史数据
      this.getOperateHistory(res.id)
      this.settingVisible = true
    },

    //  获取设置累计值页面数据
    getRowDetail (employeeId, incomeDateStr, withholdingAgent) {
      var obj = {
        employeeId: employeeId,
        incomeDate: incomeDateStr,
        withholdingAgent: withholdingAgent
      }
      this.$http({
        url: '/employeecumulativededuct/pageDetail',
        method: 'post',
        data: this.$qs.stringify(obj)
      }).then(({ data }) => {
        if (data.code == 0) {
          var res = data.data
          if (res) {
            this.settingForm.deductIdLast = res.id // 记录id
            this.settingForm.childEducationLast = res.childEducation // 累计子女教育
            this.settingForm.continueEducationLast = res.continueEducation // 累计继续教育
            this.settingForm.cumulativeSpecialDeduction = res.cumulativeSpecialDeduction // 累计减除费用
            this.settingForm.houseLoanLast = res.houseLoan // 累计住房贷款利息
            this.settingForm.houseRentLast = res.houseRent // 累计住房租金
            this.settingForm.medicalCareLast = res.medicalCare // 大病医疗
            this.settingForm.payableIncomeTaxLast = res.payableIncomeTax // 累计应纳税所得额（上月）
            this.settingForm.payedTaxAdvanceLast = res.payedTaxAdvance // 累计已预缴税额（上月）
            this.settingForm.supportOldLast = res.supportOld // 累计赡养老人
            this.settingForm.totalReductionAmountLast = res.totalReductionAmount // 累计减除费用
          }
        } else {
          this.$message({
            message: this.$global.errorMsg,
            type: 'error'
          })
        }
      })
    },

    // 获取修改历史数据
    getOperateHistory (deductId) {
      var obj = {
        deductId: deductId
      }
      this.$http({
        url: '/employeecumulativededuct/operateHistory',
        method: 'post',
        data: this.$qs.stringify(obj)
      }).then(({ data }) => {
        if (data.code == 0) {
          this.operateHistory = data.data || []
        } else {
          this.$message({
            message: this.$global.errorMsg,
            type: 'error'
          })
        }
      }).catch((data) => {
        this.$message({
          message: '系统异常',
          type: 'error'
        })
      })
    },

    // 校验金额
    validAmount (rule, value, callback) {
      if (value == '' || value == null) {
        this.settingForm[rule.field] = 0
        callback()
      } else {
        if (/^\d+$/.test(value) == false) {
          callback(new Error('请输入数字'))
        } else {
          callback()
        }
      }
    },

    //  校验 （1、	累计减除费用、累计减除费用（上月）值只能是5000的倍数，包括0；否则需提示：请输入5000的倍数）
    checkReduct (rule, value, callback) {
      if (Number(value) % 5000 != 0) {
        callback(new Error('请输入5000的倍数'))
      } else {
        var field = rule.field
        //上月
        if (field.indexOf('Last') > -1) {
          this.settingForm.totalReductionAmount = value + 5000
        } else {
          if (value > 0) {
            this.settingForm.totalReductionAmountLast = value - 5000
          }
        }
        callback()
      }
    },

    //  校验  （2、	累计子女教育是1000的倍数；否则需提示：请输入1000的倍数）
    checkEdu (rule, value, callback) {
      if (Number(value) % 1000 != 0) {
        callback(new Error('请输入1000的倍数'))
      } else {
        callback()
      }
    },

    //  校验  （2、	累计住房贷款利息是1000的倍数；否则需提示：请输入1000的倍数）
    //   （3、	同一所属期累计住房贷款利息与累计住房租金只能二选一存值，否则提示：住房贷款利息与住房租金只能二选一，请检查数据
    checkHouse (rule, value, callback) {
      console.log(value)
      var field = rule.field
      if (field.indexOf('Last') > -1) {
        //  上月  houseLoanLast houseRentLast
        if (value == 0) {
          if (field == 'houseRentLast' && this.settingForm.houseLoanLast != 0) {
            this.$refs.settingForm.validateField('houseLoanLast')
          } else if (field == 'houseLoanLast' && this.settingForm.houseRentLast != 0) {
            this.$refs.settingForm.validateField('houseRentLast')
          }
        } else if (this.settingForm.houseLoanLast > 0 && this.settingForm.houseRentLast > 0) {
          callback(new Error('住房贷款利息与住房租金只能二选一，请检查数据'))
        }
      } else {
      //  当月
        if (value == 0) {
          if (field == 'houseRent' && this.settingForm.houseLoan != 0) {
            this.$refs.settingForm.validateField('houseLoan')
          } else if (field == 'houseLoan' && this.settingForm.houseRent != 0) {
            this.$refs.settingForm.validateField('houseRent')
          }
        } else if (this.settingForm.houseLoan > 0 && this.settingForm.houseRent > 0) {
          // 当月
          callback(new Error('住房贷款利息与住房租金只能二选一，请检查数据'))
        }
      }
      if (field.indexOf('houseLoan') > -1 && Number(value) % 1000 != 0) {
        callback(new Error('请输入1000的倍数'))
      }
      callback()
    },

    //  保存-设置累计值
    saveSetInfo () {
      this.$refs.settingForm.validate((valid) => {
        this.settingBtnLoading = true
        if (!valid) {
          this.settingBtnLoading = false
        } else {
        //  保存
          this.$http({
            url: '/employeecumulativededuct/save',
            method: 'post',
            data: this.settingForm
          }).then(({ data }) => {
            this.settingBtnLoading = false
            if (data.code == 0) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.settingVisible = false
              this.getTableData()
            } else {
              this.$message({
                message: this.$global.errorMsg,
                type: 'error'
              })
            }
          }).catch((data) => {
            this.settingBtnLoading = false
            this.$message({
              message: '系统异常',
              type: 'error'
            })
          })
        }
      })
    }
  }
}
</script>
<style lang="less">
  .setting-spl-dialog{
    .el-dialog__body{
      padding: 0 0 0 15px!important;
      .pay-form-item{
        margin-bottom: 10px;
        .el-input__inner {
          text-align: right;
        }
      }
      .set-input{
        width: 150px;
        text-align: right;
      }
      .el-timeline{
        padding-left: 10px;
        .el-timeline-item__node{
          background-color: #328EDB;
        }
        .el-timeline-item__wrapper{
          padding-right: 10px;
        }
      }
    }
    .import-dialog{
      .el-upload, .el-upload-dragger{
        width: 100%;
      }
      .text{
        margin-top: 5px;
      }
    }
  }

  .import-spl-dialog{
    .el-dialog__body {
      padding: 0 15px !important;
    }
  }
  .setting-r{
    .el-card__body{
      padding: 10px;
    }
  }
</style>
<style lang="less" scoped>
  .emp-container {
    padding: 20px;
    position: relative;
    height: 100%;
  }
  .setting-l{
    float: left;
    width: 900px;
    border-right: 1px solid #dbdbdb;
  }
  .setting-r{
    float: left;
    width: 465px;
    padding: 20px 0px 20px 10px;
    max-height: 730px;
    overflow: auto;
  }
  .headImg-info{
    border-bottom: 1px solid #dbdbdb;
    padding: 5px 15px;
    margin-left: -10px;
  }
  .headImg{
    width: 65px;
    height: 65px;
    border-radius: 65px;
  }
  .tag{
    display: inline-block;
    color: #FF8400;
    border: 1px solid #FF8400;
    border-radius: 20px;
    padding: 0 10px;
    font-size: 12px;
    margin-left: 30px;
  }
  .border-type{
    border: 1px solid #dbdbdb;
    border-radius: 4px;
    padding: 10px;
    padding-bottom: 5px;
  }
</style>
